MyBatis 批量插入、删除和更新

批量插入

1
2
3
4
5
6
7
8
9
10
11
12
<insert id="insertByBatch" parameterType="list" keyProperty="id" useGeneratedKeys="true">
insert into product_list (product_id, national_code, product_name,
product_describe, comment_link, add_comment_link
)
values
<foreach collection="list" index="index" item="item" separator=",">
(
#{item.productId},#{item.nationalCode},#{item.productName},
#{item.productDescribe},#{item.commentLink},#{item.addCommentLink}
)
</foreach>
</insert>
1
void insertByBatch(@Param("list") List<ProductList> productLists);

对于foreach标签的解释参考了网上的资料,具体如下:

foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合。

foreach元素的属性主要有 item,index,collection,open,separator,close。

  • item表示集合中每一个元素进行迭代时的别名
  • index指定一个名字,用于表示在迭代过程中,每次迭代到的位置
  • open表示该语句以什么开始
  • separator表示在每次进行迭代之间以什么符号作为分隔符
  • close表示以什么结束

在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况 下,该属性的值是不一样的,主要有一下3种情况:

  1. 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
  2. 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
  3. 如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map

使用批量插入执行的SQL语句应该等价于:

1
2
insert into redeem_code (batch_id, code, type, facevalue,create_user,create_time)
values (?,?,?,?,?,? ),(?,?,?,?,?,? ),(?,?,?,?,?,? ),(?,?,?,?,?,? )

批量删除

1
2
3
4
5
6
7
<delete id="deleteByBatch" parameterType="java.lang.String">
delete from product_list
where id IN
<foreach collection="deleteIdArray" item="productId" open="(" separator="," close=")">
#{productId}
</foreach>
</delete>
1
int deleteByBatch(@Param("deleteIdArray") List<String> deleteIdArray);

批量更新

单个字段

1
2
3
4
5
6
7
8
9
10
11
12
13
<update id="updateByBatch" parameterType="java.util.List">
update t_goods
set NODE_ID=
<foreach collection="list" item="item" index="index"
separator=" " open="case" close="end">
when GOODS_ID=#{item.goodsId} then #{item.nodeId}
</foreach>
where GOODS_ID in
<foreach collection="list" index="index" item="item"
separator="," open="(" close=")">
#{item.goodsId,jdbcType=BIGINT}
</foreach>
</update>

单个字段方法二

1
2
3
4
5
6
7
8
9
10
11
12
13
<update id="updateByBatch" parameterType="java.util.List">
UPDATE
t_goods
SET NODE_ID = CASE
<foreach collection="list" item="item" index="index">
WHEN GOODS_ID = #{item.goodsId} THEN #{item.nodeId}
</foreach>
END
WHERE GOODS_ID IN
<foreach collection="list" index="index" item="item" open="(" separator="," close=")">
#{item.goodsId}
</foreach>
</update>

多个字段

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<update id="updateBatch" parameterType="java.util.List">
update t_user
<trim prefix="set" suffixOverrides=",">
<trim prefix="STATUS =case" suffix="end,">
<foreach collection="list" item="i" index="index">
<if test="i.status!=null">
when USER_ID=#{i.userId} then #{i.status}
</if>
</foreach>
</trim>
<trim prefix=" OPERATE_TIME =case" suffix="end,">
<foreach collection="list" item="i" index="index">
<if test="i.operateTime!=null">
when USER_ID=#{i.userId} then #{i.operateTime}
</if>
</foreach>
</trim>

<trim prefix="OPERATOR =case" suffix="end," >
<foreach collection="list" item="i" index="index">
<if test="i.operator!=null">
when USER_ID=#{i.userId} then #{i.operator}
</if>
</foreach>
</trim>
</trim>
where
<foreach collection="list" separator="or" item="i" index="index" >
USER_ID=#{i.userId}
</foreach>
</update>
1
int updateBatch(List<WaterEle> list);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<update id="updateByBatch" parameterType="java.util.List">
update product_list
<trim prefix="set" suffixOverrides=",">
<trim prefix="national_code=case" suffix="end,">
<foreach collection="list" item="i" index="index">
<if test="i.nationalCode!=null">
when id=#{i.id} then #{i.nationalCode}
</if>
</foreach>
</trim>
<trim prefix="product_name =case" suffix="end,">
<foreach collection="list" item="i" index="index">
<if test="i.productName!=null">
when id=#{i.id} then #{i.productName}
</if>
</foreach>
</trim>
<trim prefix="product_describe=case" suffix="end," >
<foreach collection="list" item="i" index="index">
<if test="i.productDescribe!=null">
when id=#{i.id} then #{i.productDescribe}
</if>
</foreach>
</trim>
<trim prefix="comment_link=case" suffix="end," >
<foreach collection="list" item="i" index="index">
<if test="i.commentLink!=null">
when id=#{i.id} then #{i.commentLink}
</if>
</foreach>
</trim>
<trim prefix="add_comment_link=case" suffix="end," >
<foreach collection="list" item="i" index="index">
<if test="i.addCommentLink!=null">
when id=#{i.id} then #{i.addCommentLink}
</if>
</foreach>
</trim>
</trim>
where
<foreach collection="list" separator="or" item="i" index="index" >
id=#{i.id}
</foreach>
</update>
1
void updateByBatch(@Param("list") List<ProductList> productLists);